package ee.ut.algorithmics.project;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadDocument {

	public static Record readDoc(String documentName) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(new File(
				documentName)));
		String line;
		StringBuilder sBuilder = new StringBuilder();
		while ((line = reader.readLine()) != null) {
			sBuilder.append(line);
		}
		Record record = createRecord(sBuilder.toString());
		return record;
	}

	private static Record createRecord(String document) {
		String[] tokens = document.trim().split(" ");
		Record record = new Record();
		for (int i = 0; i < tokens.length; i++) {
			String token = tokens[i].trim().toLowerCase();
			String[] split = null;
			if (token.length() == 0) {
				continue;
			}
			if (token.contains(",")) {
				split = token.split(",");
			}
			if (token.contains("-")) {
				split = token.split("-");
			}
			if (token.contains("'")) {
				token = token.replace("'", "");
			}
			if (token.contains("\"")) {
				split = token.split("\"");
			}
			if (token.contains(":")) {
				split = token.split(":");
			}
			if (token.contains(";")) {
				split = token.split(";");
			}
			if (token.contains(".")) {
				split = token.split("\\.");
			}
			if (token.contains("?")) {
				split = token.split("?");
			}
			if (token.contains("!")) {
				split = token.split("!");
			}
			if (split != null) {
				for (int j = 0; j < split.length; j++) {
					String subToken = split[j].trim();
					if (subToken.length() > 0) {
						record.addToken(subToken);
					}
				}
			} else {
				record.addToken(token);
			}
		}
		return record;
	}
}
